package com.dmxh.service.impl;

import com.dmxh.mapper.TbAddressMapper;
import com.dmxh.pojo.TbAddress;
import com.dmxh.service.AddressService;
import com.dmxh.vo.AddressVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private TbAddressMapper addressMapper;

    @Override
    public Map<String, Object> queryAddress() {
        List<TbAddress> tbAddresses = this.addressMapper.selectByExample(null);
        List<AddressVo> collect = tbAddresses.stream().map(addresslist -> {
            AddressVo addressVo = new AddressVo();
            BeanUtils.copyProperties(addresslist, addressVo);
            return addressVo;
        }).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("addresslist", collect);
        return map;
    }

    @Override
    public boolean saveAddress(AddressVo addressVo) {
        TbAddress tbAddress = new TbAddress();
        BeanUtils.copyProperties(addressVo, tbAddress);
        return this.addressMapper.insertSelective(tbAddress) == 1 ? true : false;
    }

    @Override
    public boolean updateAddress(AddressVo addressVo) {
        TbAddress tbAddress = new TbAddress();
        BeanUtils.copyProperties(addressVo, tbAddress);
        return this.addressMapper.updateByPrimaryKeySelective(tbAddress) == 1 ? true : false;
    }

    @Override
    public boolean deleteAddress(String id) {/*确保精度不会丢失这样写  万一前端传过来的是字符串类型的id  那我们在后面也能给他转成Long类型*/
        return this.addressMapper.deleteByPrimaryKey(Long.parseLong(id)) == 1 ? true : false;
    }

    @Override
    public AddressVo addressByIdQueryAll(Long addressId) {
        TbAddress tbAddress = this.addressMapper.selectByPrimaryKey(addressId);
        AddressVo addressVo = new AddressVo();
        BeanUtils.copyProperties(tbAddress, addressVo);
        return addressVo;
    }
}
